package org.llt.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * redis 测试各种方法通过这个 controller 提供的 http 接口来触发
 **/
@Slf4j
@RestController
public class MyController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/string")
    public String testString() {
        // 清空数据库, 使用原生的命令
        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForValue().set("key1", "111");
        redisTemplate.opsForValue().set("key2", "222");
        redisTemplate.opsForValue().set("key3", "333");

        String key1 = redisTemplate.opsForValue().get("key1");

        log.info("key 的值为: {}", key1);

        return "ok";
    }

    @GetMapping("/list")
    public String testList() {
        // 清空数据库, 使用原生的命令
        redisTemplate.execute((RedisConnection connection)->{
           connection.flushAll();
           return null;
        });


        redisTemplate.opsForList().leftPush("key1", "111");
        redisTemplate.opsForList().leftPush("key1", "222");
        redisTemplate.opsForList().leftPush("key1", "333");

        String value = redisTemplate.opsForList().rightPop("key1");
        System.out.println("value: " + value);
        value = redisTemplate.opsForList().rightPop("key1");
        System.out.println("value: " + value);
        value = redisTemplate.opsForList().rightPop("key1");
        System.out.println("value: " + value);


        return "ok";
    }

    @GetMapping("/set")
    public String testSet() {
        // 清空数据库, 使用原生的命令
        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key1", "111", "222", "333");

        System.out.println("key1: " + redisTemplate.opsForSet().members("key1"));

        System.out.println("key1 的数量: " + redisTemplate.opsForSet().size("key1"));

        return "ok";
    }

    @GetMapping("/hash")
    public String testHash() {
        redisTemplate.execute((RedisConnection connection) -> {
           connection.flushAll();
           return null;
        });

        redisTemplate.opsForHash().put("key1", "f1", "111");
        redisTemplate.opsForHash().put("key1", "f2", "222");
        redisTemplate.opsForHash().put("key1", "f3", "333");

        System.out.println("key1 中 f1 映射的值: " + redisTemplate.opsForHash().get("key1", "f1"));
        System.out.println("key1 是否存在 f1: " + redisTemplate.opsForHash().hasKey("key1", "f1"));
        System.out.println("删除 key1 中 f1，f2：" + redisTemplate.opsForHash().delete("key1", "f1", "f2"));
        System.out.println("key1 field 个数: " + redisTemplate.opsForHash().size("key1"));

        return "ok";
    }

    @GetMapping("zset")
    public String tsetZset() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("key1", "llt", 18.0);

        return "ok";
    }



}
